<template>
	<view class="uni-common-mt" style="padding: 30upx 20upx;background-color: #CCCCCC;">
		<uni-popup :show="showPop" position="top" mode="fixed" v-bind:msg="popMsg" @hidePopup="togglePopup(false)" />
		<view class="uni-form-item uni-column">
			<view class="title">交易金额</view>
			<input class="uni-input" v-model="amt" type="digit" placeholder="请输入交易金额" />
		</view>
		<view class="uni-form-item uni-column">
			<view class="uni-list">
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						选择费率
					</view>
					<view class="uni-list-cell-db">
						<picker @change="ratePickerChange" :value="index" :range="rateArray" range-key="name">
							<view class="uni-input">{{rateArray[index].name}} | {{rateArray[index].rate}} | {{rateArray[index].charge}}</view>
						</picker>
					</view>
				</view>
			</view>
			
		</view>
		<view class="uni-form-item uni-column">
			<view class="uni-list">
				<view class="uni-list-cell">
					<view class="uni-list-cell-left">
						选择交易日期
					</view>
					<view class="uni-list-cell-db">
						<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
							<view class="uni-input">{{date}}</view>
						</picker>
					</view>
				</view>
			</view>
			
		</view>
		<view class="uni-padding-wrap uni-common-mt">
			<view class="button-sp-area">
				<button type="primary" plain="true" @click="action('1')">保存</button>
			</view>
			<view class="button-sp-area" style="padding-top: 20upx;">
				<button type="warn" plain="true" @click="action('2')">删除</button>
			</view>
		</view>
	</view>
</template>

<script>
	function getDate(type) {
		const date = new Date();
	
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();
	
		if (type === 'start') {
			year = year - 60;
		} else if (type === 'end') {
			year = year + 2;
		}
		month = month > 9 ? month : '0' + month;;
		day = day > 9 ? day : '0' + day;
	
		return `${year}-${month}-${day}`;
	}
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	export default {
		data() {
			return {
				index:0,
				showPop:false,
				popMsg:'',
				rateArray:[],
				date: getDate({
					format: true
				}),
				startDate:getDate('start'),
				endDate:getDate('end'),
				amt: '', 
				id:'',
			}
		},
		components:{
			uniPopup
		},
		onLoad(options) {
			this.id = options.id;
			this.amt = options.amt;
			this.date = options.transDate;
		},
		onShow() {
			this.initCostData();
		},
		methods: {
			initCostData(){
				const that = this;
				this.$net.getCostList().then(res => {
					const rateArray = res.data;
					rateArray.sort((a,b)=>{
						if(a.defaultType === b.defaultType){
							return parseInt(b.id) - parseInt(a.id);
						}else{
							return parseInt(b.defaultType) - parseInt(a.defaultType);
						}
					})
					// console.log(rateArray);
					that.rateArray = rateArray;
				})
			},
			action(type){
				const that = this;
				// this.$api.msg(this.amt);
				if(!Boolean(this.amt)){
					this.popMsg = '请输入交易金额';
					this.togglePopup(true);
					return;
				}
				if(type === '1'){
					this.$net.updateTrade({
						amt:this.amt,
						transDate:this.date,
						rate:this.rateArray[this.index].rate,
						charge:this.rateArray[this.index].charge,
						id:this.id,
					}).then(res => {
						if(res.code === 0){
							uni.navigateBack({
								delta:1
							})
						}
					})
				}else{
					this.$net.deleteTrade({
						id:this.id
					}).then(res => {
						// console.log(res);
						if(res.code === 0){
							uni.navigateBack({
								delta:1
							})
						}
					})
				}
				
			},
			togglePopup(type){
				this.showPop = type;
			},
			ratePickerChange(e){
				this.index = e.detail.value;
			},
			bindDateChange: function(e) {
				this.date = e.target.value
			},
		}
	}
</script>

<style>
	page {
		height: auto;
		min-height: 100%;
		background-color: #EBEBEB;
	}
</style>
